using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
public class Bar39
{
  //Random rnd=new Random();
  Pen thinPen=new Pen(Color.Black,0.1f);
  Font verd(int size) { return new Font("verdana",size); }
  float narrow=0.6f;
  float wide=1.5f;
  int height=20;
  string data;
  string encodedString;
  static readonly string alphabet39="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*";
  static readonly string[] coded39Char= 
		{
			/* 0 */ "000110100", 
			/* 1 */ "100100001", 
			/* 2 */ "001100001", 
			/* 3 */ "101100000",
			/* 4 */ "000110001", 
			/* 5 */ "100110000", 
			/* 6 */ "001110000", 
			/* 7 */ "000100101",
			/* 8 */ "100100100", 
			/* 9 */ "001100100", 
			/* A */ "100001001", 
			/* B */ "001001001",
			/* C */ "101001000", 
			/* D */ "000011001", 
			/* E */ "100011000", 
			/* F */ "001011000",
			/* G */ "000001101", 
			/* H */ "100001100", 
			/* I */ "001001100", 
			/* J */ "000011100",
			/* K */ "100000011", 
			/* L */ "001000011", 
			/* M */ "101000010", 
			/* N */ "000010011",
			/* O */ "100010010", 
			/* P */ "001010010", 
			/* Q */ "000000111", 
			/* R */ "100000110",
			/* S */ "001000110", 
			/* T */ "000010110", 
			/* U */ "110000001", 
			/* V */ "011000001",
			/* W */ "111000000", 
			/* X */ "010010001", 
			/* Y */ "110010000", 
			/* Z */ "011010000",
			/* - */ "010000101", 
			/* . */ "110000100", 
			/*' '*/ "011000100",
			/* $ */ "010101000",
			/* / */ "010100010", 
			/* + */ "010001010", 
			/* % */ "000101010", 
			/* * */ "010010100" 
		};
  public Bar39(string data) : this(data,1,3) { }
  public Bar39(string data,float narrow,float wide)
  {
    this.narrow=narrow;
    this.wide=wide;
    data=data.ToUpper();
    for(int i=0;i<data.Length;i++)
      if(alphabet39.IndexOf(data[i])==-1||data[i]=='*')
        throw new Exception("invalid character:"+data[i]);
    this.data=data.ToUpper();
    String intercharacterGap="0";
    String str='*'+data+'*';
    for(int i=0;i<str.Length;i++)
    {
      if(i>0)
        encodedString+=intercharacterGap;
      encodedString+=coded39Char[alphabet39.IndexOf(str[i])];
    }
  }
  public float Width { get { return (6+data.Length*3)*wide+(13+data.Length*7)*narrow; } }

  public void Paint(Graphics g,int left,int top)
  {

    float x=left;
    float wid=0;
    for(int i=0;i<encodedString.Length;i++)
    {
      wid=(encodedString[i]=='1')?wide:narrow;
      if(i%2==0)
        g.FillRectangle(Brushes.Black,x,top,wid,height);
      x+=wid;
    }
    g.DrawLine(thinPen,left,top-1,left+Width,top-1);
    g.DrawLine(thinPen,left,top+height+1,left+Width,top+height+1);
    g.DrawString(data,verd(9),Brushes.Black,left,top+height-1);
    g.DrawString(data+"(n/w:"+narrow.ToString("0.00")+"/"+wide.ToString("0.00")+"), wid:"+Width.ToString("0.00"),verd(3),Brushes.Black,left,top+height+12);

  }
}
